package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.mapper.QuestionContentMapper;
import cn.wolfcode.luowowo.article.mapper.QuestionMapper;
import cn.wolfcode.luowowo.article.query.QuestionQuery;
import cn.wolfcode.luowowo.article.service.IAnswerService;
import cn.wolfcode.luowowo.article.service.IQuestionService;
import cn.wolfcode.luowowo.cache.service.IAnswerRedisService;
import cn.wolfcode.luowowo.cache.service.IQuestionRedisService;
import cn.wolfcode.luowowo.cache.vo.AnswerStatisVO;
import cn.wolfcode.luowowo.cache.vo.QuestionStatisVO;
import cn.wolfcode.luowowo.common.util.RegexUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

/**
 * @author 名字
 * @company 公司
 * @create 2019-09-25 22:12
 */
@Service
public class QuestionServiceImpl implements IQuestionService {

    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private QuestionContentMapper contentMapper;

    @Autowired
    private IAnswerService answerService;

    @Reference
    private IQuestionRedisService questionRedisService;

    @Reference
    private IAnswerRedisService answerRedisService;

    @Override
    public Question get(Long id) {
        return questionMapper.selectByPrimaryKey(id);
    }

    @Override
    public QuestionContent getContent(Long id) {
        return contentMapper.selectByPrimaryKey(id);
    }

    @Override
    public void saveOrUpdate(Question question, Long[] tags) {

        //维护关系

        if (question.getId() != null) {
            questionMapper.updateByPrimaryKey(question);

            //设置内容
            QuestionContent questionContent = question.getQuestionContent();
            questionContent.setId(question.getId());

            contentMapper.updateByPrimaryKey(questionContent);
            questionMapper.deleteRelation(question.getId());


        } else {
            //设置创建时间
            question.setCreateTime(new Date());
            questionMapper.insert(question);
            //设置内容
            QuestionContent questionContent = question.getQuestionContent();
            questionContent.setId(question.getId());
            contentMapper.insert(questionContent);
        }

        if (tags != null) {
            for (long tag : tags) {
                //添加中间表
                questionMapper.insertRelation(question.getId(), tag);
            }
        }


    }

    @Override
    public PageInfo query(QuestionQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<Question> list = questionMapper.selectForList(qo);
        for (Question question : list) {
            //查询该提问下的第一个回答
            Answer answer = answerService.getTop1(question.getId());
            if (answer != null) {
                //填充内容
                AnswerContent content = answerService.getContent(answer.getId());
                //截取第一张图片
                //截取200个字
                if (content != null) {
                    String content1 = content.getContent();
                    content.setContent(RegexUtil.getImgStr(content1) + "<p>" + RegexUtil.removeHtmlImg(content1) + "</p>");
                    answer.setContent(content);
                }
                List<Answer> answers = question.getAnswers();
                AnswerStatisVO statisVo = answerRedisService.getStatisVo(answer.getId());
                answer.setThumsupnum(statisVo.getThumbsupnum());
                answers.add(answer);
                question.setAnswers(answers);
            }


            //从vo对象中取出设置进去
            QuestionStatisVO statisVo = questionRedisService.getStatisVo(question.getId());
            question.setAnswernum(statisVo.getReplynum());
            question.setViewnum(statisVo.getViewnum());


        }

        return new PageInfo(list);
    }
}
